package and.activity;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.gfan.sdk.statitistics.GFAgent;

public class BaseActivityGroup extends ActivityGroup{
	
	@Override
	protected void onResume() {
		GFAgent.onResume(this);
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		GFAgent.onPause(this);
		super.onPause();
	}
	
	private Intent myIntent;
	private Class<?> cls;
	public void setCls(Class<?> cls){
		this.cls = cls;
		
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.myactivitygroup);//
		LinearLayout container=(LinearLayout) findViewById(R.id.myactivitygroup_view);//
        container.removeAllViews();
        this.myIntent = new Intent(this,this.cls);
        myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		Window subActivity=getLocalActivityManager().startActivity("subActivity",myIntent);
		View view=subActivity.getDecorView();
		container.addView(view);  //下面的代码是为了保证MainActivity显示正常，必须写在这行后面，不然的话，view对象尚未产生，addView应该也是用InFlate实现的
		LayoutParams params=(LayoutParams) view.getLayoutParams();
		params.width=LayoutParams.FILL_PARENT;
		params.height=LayoutParams.FILL_PARENT;
		view.setLayoutParams(params);
	}
		
	public static void changeActivity(Activity from,String to) throws Exception{
		LinearLayout container=(LinearLayout)((ActivityGroup)from.getParent()).getWindow().findViewById(R.id.myactivitygroup_view);//注意这里，还是获取group的view
        container.removeAllViews();
        Intent intent=new Intent(from,Class.forName("and.activity."+to));
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		Window subActivity=((ActivityGroup)from.getParent()).getLocalActivityManager().startActivity(to,intent);
		View view=subActivity.getDecorView();
		container.addView(view);
		LinearLayout.LayoutParams params=(LayoutParams) view.getLayoutParams();
		params.width=LayoutParams.FILL_PARENT;
		params.height=LayoutParams.FILL_PARENT;
		view.setLayoutParams(params);
	}
}
